//
//  HuanKuanCouponsViewController.m
//  BrushFace
//
//  Created by 徐雪姣 on 16/3/8.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "HuanKuanCouponsViewController.h"
#import "MyConCell.h"
#import "NavHelper.h"

@interface HuanKuanCouponsViewController ()<UITableViewDataSource,UITableViewDelegate>
{
      UITableView *_tableView;
      NSMutableArray *_dataArray;
      UILabel *headerlabel;
      NSDictionary *_currentDict;
}

@end

@implementation HuanKuanCouponsViewController

- (void)viewDidLoad{
      [super viewDidLoad];
      [self UIConfig];
      [self dataInit];
      
      _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped]
      ;
      _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
      _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
      _tableView.delegate = self;
      _tableView.dataSource = self;
      [self.view addSubview:_tableView];
}

- (void)UIConfig{
      [self.navBar setTitle:@"优惠券"];
      [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
      
      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 80, 44)];
      [button setTitle:@"使用说明" forState:UIControlStateNormal];
      [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
      button.titleLabel.font = [UIFont systemFontOfSize:13];
      [button addTarget:self action:@selector(rightBtnClikc) forControlEvents:UIControlEventTouchUpInside];
      [self.navBar setRightView:button];
      
}

- (void)dataInit{
      _dataArray = [[NSMutableArray alloc] init];
      
      NSString *requestStr = [NSString stringWithFormat:@"%@/hope/getlendcouponslist.json",SERVICE_ADRESS];
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      [manager GET:requestStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
            _currentDict = jsondict;
          
            [_dataArray removeAllObjects];
            [_dataArray addObjectsFromArray:jsondict[@"data"]];
            
            [_tableView reloadData];
            
      } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
            
      }];
}

#pragma mark - tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      MyConCell *cell = [MyConCell loadBundleNib];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      NSDictionary *subDict = [_dataArray objectAtIndex:indexPath.row];
      cell.titlelabel.text = subDict[@"name"];
      NSString *limitstr = subDict[@"limitstr"];
      cell.moneyLabel.text = [NSString stringWithFormat:@"¥%@",[subDict objectForKey:@"couponsvalue"]];
      NSString *finalStr = [limitstr stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
      cell.limitLabel.text = finalStr;
      cell.dateLabel.text = [NSString stringWithFormat:@"%@",subDict[@"validity"]];
      return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
      return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
      UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
      view.backgroundColor = [UIColor clearColor];
      headerlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 35)];
      headerlabel.font = [UIFont systemFontOfSize:14];
      headerlabel.textColor = [UIColor grayColor];
      headerlabel.textAlignment = NSTextAlignmentCenter;
      if (_currentDict.count>0) {
            headerlabel.text = [NSString stringWithFormat:@"共 %@ 张",_currentDict[@"totalRecords"]];
            
      }
      [view addSubview:headerlabel];
      
      UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, SCREEN_WIDTH, 1)];
      line.textColor = [UIColor lightGrayColor];
      [view addSubview:line];
      return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
      return 120;
}
- (void)rightBtnClikc{
      [NavHelper gotoInstructionsWithConpousVC];
      
      
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
